<?php

class SurveyController extends Controller {
	
	public $layout='//layouts/column2';
	
	public function beforeAction($action)
	{
		$config = array();
		switch ($action->id)
		{
			case 'survey':
				$config = array(
					'steps'=> array('goal','field','status','value','keyResources'),
					'events'=>array(
						'onStart'=>'wizardStart',
						'onProcessStep'=>'surveyWizardProcessStep',
						'onFinished'=>'surveyFinished',
						'onInvalidStep'=>'wizardInvalidStep',
						'onCancelled'=>'surveyCancelled'
					)
				);
				break;
								
			case 'bcase':
				$config = array(
					'steps'=> array('Business Case'=>'businessCase', 'Business'=>'business','Business Model'=>'businessModel'),
					'events'=> array(
						'onStart'=>'wizardStart',
						'onProcessStep'=>'bcaseWizardProcessStep',
						'onFinished'=>'bcaseFinished'
					)
				);
				break;
				
		}
		if (!empty($config)){
			$config['class']='application.extensions.WizardBehavior';
			$this->attachBehavior('wizard',$config);
		}
		return parent::beforeAction($action);
	}
	
	public function actionSurvey($step=null) {
		$this->pageTitle = 'Survey Wizard';
		$this->process($step);
	}
	
	public function actionBcase($step=null) {
		$this->process($step);
	}	
	
	// Wizard Behavior Event Handlers
	/**
	* Raised when the wizard starts; before any steps are processed.
	* MUST set $event->handled=true for the wizard to continue.
	* Leaving $event->handled===false causes the onFinished event to be raised.
	* @param WizardEvent The event
	*/
	public function wizardStart($event) {
		$event->handled = true;
	}
	
/**
	* Raised when the wizard detects an invalid step
	* @param WizardEvent The event
	*/
	public function wizardInvalidStep($event) {
		Yii::app()->getUser()->setFlash('notice', $event->step.' is not a vaild step in this wizard');
	}	
	
	public function surveyWizardProcessStep($event)
	{
		$modelName = ucfirst($event->step);
		$model = new $modelName();
		$model->attributes = $event->data;
		$form = $model->getForm();
		
		if ($form->submitted() && $form->validate()) {
			$event->sender->save(empty($model->attributes)?array():$model->attributes);
			$event->handled = true;
		}
		else
			$this->render('form',compact('event','form'));
	}
	
	public function bcaseWizardProcessStep($event)
	{
		$modelName = ucfirst($event->step);
		$model = new $modelName();
		$model->attributes = $event->data;
		$form = $model->getForm();
		
		if ($form->submitted() && $form->validate()) {
						
			if ($event->step == 'business')
			{			
				$businessResources = $_POST['Business']['tblBusinessresources'];
				$valueProposition = $_POST['Business']['tblValuepropositions'];
				$attributes[0] = $model->attributes;
				$attributes[1] = $businessResources;
				$attributes[2] = $valueProposition;
				
				$event->sender->save(empty($attributes)?array():$attributes);
			}
			else 
			{
				$event->sender->save(empty($model->attributes)?array():$model->attributes);
			}
			
			$event->handled = true;
		}
		else
			$this->render('formBCase',compact('event','form'));
	}
	
	
	public function surveyFinished($event) {
		
		$i = 0;
		$business = new Business();
		foreach ($event->data as $step=>$data) {
			foreach ($data as $k=>$v)
			{
				switch ($i) {
					case 0: $business->businessGoal = $v;
					break;
					case 1: $business->economicSector_id = $v;
					break;
					case 2: $business->businessSector_id = $v;
					break;
					case 3: $business->industryGroup_id = $v;
					break;
					case 4: $business->businessStatus = $v;
					break;
					case 5: $business->valueProposition = $v;
					break;
					case 6: $business->businessResources = $v;
					break;
					default:
						;
					break;
				}
				$i++;
							
			}
		}
		$businessArray = $business->searchSameModel();
		
		$businessCaseArray = array();
		/*$businessCaseArray = $businessArray[0]->businessCases;
		$businessCaseArray = BusinessCase::model()->findAllByAttributes(
        				array('business_id' => $businessArray[0]->id));*/
        				
		foreach ($businessArray as $business) {
			$businessCaseArray = array_merge($businessCaseArray, $business->businessCases);
		}
		
		$businessModelArray = array();
		
		foreach ($businessCaseArray as  $businessCase) {
			foreach ($businessModelArray as  $businessModel) {
					if($businessModel->id == $businessCase->businessModel_id) {
						$flag = 1;
						break;
					}
			}
			if($flag != 1){
					array_push($businessModelArray, $businessCase->businessModel);					
			}
			$flag = 0;
		}
		
		$dataProviderModel=new CArrayDataProvider($businessModelArray, array(
    				'pagination'=>array(
      						'pageSize'=>10,
   						 ),
				));
		
		$this->render('businessModelView',array(
							'dataProvider'=>$dataProviderModel,
							'businessCaseArray'=>$businessCaseArray
					));
		
		$event->sender->reset();
		Yii::app()->end();
	}	
	
	public function bcaseFinished($event) {
		
		
		$business = new Business();
		$bAttributes = $event->data['business'];

		$business->attributes = $bAttributes[0];
		$business->tblBusinessresources = $bAttributes[1];
		$business->tblValuepropositions = $bAttributes[2];
		//$business->save();
		//$this->render('/business/_view', array('model'=>$business));
		//$this->render('test',array('a'=>$business));
		
		
		$businessModel = new BusinessModel();
		$businessModel->attributes= $event->data['businessModel'];
		
		$businessCase = new BusinessCase();
		$businessCase->attributes = $event->data['businessCase'];
		
		if ($bID = Business::model()->isRecordExists($business))
		{
			$businessCase->business_id = $bID; 
		}
		else
		{
			$business->save();
			$businessCase->business_id = $business->id;
		}
			
		if ($bModelID = BusinessModel::model()->isRecordExists($businessModel))
		{
			$businessCase->businessModel_id = $bModelID; 
		}
		else
		{
			$businessModel->save();
			$businessCase->businessModel_id = $businessModel->id;
		}	
		
		if($businessCase->save())
			$this->render('/businessCase/extendedView',array('model'=>$businessCase));
			
		$event->sender->reset();
		Yii::app()->end();
	}
	
}